var calendar = {
	config:{
		noteYear:  null,				//年
		noteMonth: null,				//月
		noteDay:   null,				//日
		active:	   null,				//选定日期
		minDate:   null,
		maxDate:   null,
		feast:[
			{time:'1-1',name:'元旦'},	//节假日
			{time:'2-14',name:'情人节'},
			{time:'3-8',name:'妇女'},
			{time:'4-5',name:'清明'},
			{time:'5-1',name:'劳动'},
			{time:'6-1',name:'儿童'},
			{time:'9-10',name:'教师'},
			{time:'10-1',name:'国庆'}
		]
	},
	main:function(name,days,min,max){				//初始化
		var _this = this,
			year = _this.today.year(),
			month = _this.today.month(),
			day = _this.today.day();

		_this.config.noteYear = year;
		_this.config.noteMonth = month;
		_this.config.noteDay = day;
		_this.config.active = days;
		_this.config.minDate = min;
		if(max===undefined){
			_this.config.maxDate = max;
		}
		
		var now = _this.tostring(year,month);
		_this.noteDown();
		var next = _this.tostring(year,month+1);
		$(name).html(now+next);
	},
	tostring:function(year,month){
		var _this = this,
			day = _this.days(year,month),
			week = _this.weeks(year,month,1),
			date=1,
			dataDom = '<div class="ord-year">'+
							year+'年'+month+'月'+
					  '</div>'+
					  '<dl class="am-cf">'+
					  	'<dt>日</dt>'+
					  	'<dt>一</dt>'+
					  	'<dt>二</dt>'+
					  	'<dt>三</dt>'+
					  	'<dt>四</dt>'+
					  	'<dt>五</dt>'+
					  	'<dt>六</dt>';
			for(i=0;i<42;i++){
				if(week<=i&&date<=day){
					var feast = _this.feast(year,month,date),
						limit = _this.limit(year,month,date),
						week = _this.weeks(year,month,date),
						active = '';
					if(!feast){
						feast=date;
					}
					if(limit){
						active= '"valid"';
					}
					var price = '<dd class='+active+
									'data-time='+year+'-'+month+'-'+date+' '+
									'data-week='+week
								+'>'+
									'<h6>'+ feast +'</h6>'+
								'</dd>';

					dataDom += price;
					date++;
				}else{
					var time ='<dd></dd>';
					dataDom +=time;
				}
			}
			dataDom +='</dl>';
			return dataDom;
	},
	feast:function(year,month,day){
		var _this = this,
			feastData = month+'-'+day,
			today = _this.today.year()+'-'+_this.today.month()+'-'+_this.today.day(),
			compare = year+'-'+month+'-'+day,
			feast = false;

		for(var i=0,num=this.config.feast.length;i<num;i++){
			if(_this.config.feast[i].time===feastData){
				feast = _this.config.feast[i].name;
				break;
			}else if(today===compare){
				feast ='今天';
			}
		}
		return feast;
	},
	limit:function(year,month,day){
		var _this = this,
			min = _this.config.minDate,
			minArry = min.split('-');
			date = new Date(year,month,day),
			minNow = new Date(minArry[0],minArry[1],minArry[2]),
			feast = false;
		if(max===undefined){
			feast = date>minNow;
		}else{
			var max = _this.config.maxDate,
				maxArry = max.split('-'),
				maxNow = new Date(maxArry[0],maxArry[1],maxArry[2]);
			feast = (date>minNow && date<maxNow);
		}
		return feast;
	},
	noteDown:function(){			//下个月
		var _this = this,
			month = _this.config.noteMonth;

		if(month===12){
			_this.config.noteMonth=1;
			_this.config.noteYear++;
		}else{
			_this.config.noteMonth++;
		}
	},
	today:{							//今天
		date:new Date(),
		year:function(){
			return this.date.getFullYear();
		},
		month:function(){
			return this.date.getMonth()+1;
		},
		day:function(){
			return this.date.getDate();
		}
	},
	days:function(year,month){		//返回某个月的总天数
		month=parseInt(month,10);
		var day=new Date(year,month,0);
		return day.getDate();
	},
	weeks:function(year,month,day){		//计算某个月的第一天是星期几
		month=month-1;
		var week=new Date(year,month,day);
		return week.getDay();
	}
}

$(function(){

	if($('.ord-page').size()>0){
		$('.ord-dl').on('click','.no-deta',function(event){
			var days = $(this).find('.ord-time').text(),
				min = $('.no-data').data('min'),
				max = $('.no-data').data('max');
			calendar.main('.ord-calendar',days,min,max);
			page.right('.page-2','show-2');
			event.stopPropagation();
		});
		$('.page-ye').on('click','.ord-return',function(event){
			var show = $(this).data('return');
			page.left('.page-1',show);
			event.stopPropagation();
		});
	}
	if($('.ord-calendar').size()>0){
		$('.ord-calendar').on('click','.valid',function(event){
			var date = $(this).data('time');
			var toTest='<h6 class="flex">'+
							'<p class="ord-time">'+
								date+
							'</p>'+
							'<span>'+'星期'+'日一二三四五六'.charAt($(this).data('week'))+'</span>'+
					   '</h6>';
			$('.no-data').next().html(toTest);
			$('#calender-date').val(date);
			page.left('.page-1','show-2');
			event.stopPropagation();
		});
	}
});

var page = {
	right:function(pageName,showName){
		$('.ord-page').addClass(showName).height($(pageName).height());
		$('.ord-foot').hide();
		$('html, body').scrollTop(0);
	},
	left:function(pageName,showName){
		$('.ord-page').removeClass(showName).height($(pageName).height());
		$('.ord-foot').show();
		$('html, body').scrollTop(0);
	}
} 